#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.DTO;
using Premiere.Window;
using System.Collections;
using Premiere.Framework;

#endregion

namespace PremiereMX
{
    public partial class UserControlCustomer : UserControl, IUserControlCustomer
    {
        CustomerLogic m_logic;
        IMainForm m_mainForm;
        Hashtable m_items = new Hashtable();
        bool b_init = false;
        Keys m_key = Keys.A;

        public UserControlCustomer(string name, IMainForm mainForm)
        {
            InitializeComponent();
            m_logic = new CustomerLogic(mainForm, this);
            m_mainForm = mainForm;
            this.Name = name;
            this.Dock = DockStyle.Fill;
            InitButtonClick();
        }
        private void InitButtonClick()
        {
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            toolBarButtonNew.Click += new EventHandler(toolBarButtonNew_Click);
            toolBarButtonPrint.Click += new EventHandler(toolBarButtonPrint_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            buttonSave.Click += new EventHandler(toolBarButtonSave_Click);
            buttonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            buttonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            buttonNew.Click += new EventHandler(toolBarButtonNew_Click);
        }
        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        void toolBarButtonPrint_Click(object sender, EventArgs e)
        {
            m_logic.Print();
        }

        void toolBarButtonNew_Click(object sender, EventArgs e)
        {
            m_logic.New();
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            m_logic.Edit();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            m_logic.Delete();
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            m_logic.Save();
        }
        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        #region IUserControlCustomer Members

        public string Code
        {
            get
            {
                return textBoxCode.Text;
            }
            set
            {
                textBoxCode.Text = value;
            }
        }

        public string Named
        {
            get
            {
                return textBoxName.Text;
            }
            set
            {
                textBoxName.Text = value;
            }
        }

        public DateTime LastUpdate
        {
            set { statusBar1.Text = statusBar1.Text + " Last Updated on : " + value.ToString("dd-MMM-yyyy"); }
        }

        public string ComputerName
        {
            set { statusBar1.Text = statusBar1.Text + " Computer Name : " + value; }
        }

        public string UserUpdateBy
        {
            set { statusBar1.Text = statusBar1.Text + " Updated By : " + value; }
        }

        public void ShowMessage(string mess)
        {
            MessageBox.Show(mess, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void UpdateList(System.Collections.IList list)
        {
            foreach (CustomerDTO curr in list)
            {
                if (m_items.ContainsKey(curr.Id))
                {
                    ListViewItem item = (ListViewItem)m_items[curr.Id];
                    item.SubItems[0].Text = curr.Code;
                    item.SubItems[1].Text = curr.Name;
                    item.SubItems[2].Text = curr.LastUpdate.ToString("dd-MMM-yyyy hh:mm:ss");
                    item.SubItems[3].Text = curr.UserName;
                }
                else
                {
                    ListViewItem item = listViewRecords.Items.Add(curr.Code);
                    item.SubItems.Add(curr.Name);
                    item.SubItems.Add(curr.LastUpdate.ToString("dd-MMM-yyyy hh:mm:ss"));
                    item.SubItems.Add(curr.UserName);
                    item.Tag = curr;
                    m_items.Add(curr.Id, item);
                }
            }
        }

        public void SetMainForm(IMainForm mainform)
        {
            m_mainForm = mainform;
        }

        public void UpdateButton()
        {
            m_mainForm.UpdateButton(toolBarMain.Buttons);
        }

        public void Init()
        {
            if (b_init) { return; } b_init = true; 
            m_logic.Init();
            this.topcomboBox1.DataSource = m_logic.TOPLost;
            employeecomboBox2.DataSource = m_logic.EmployeeList;
            taxcomboBox3.DataSource = m_logic.TaxList;
            currencycomboBox4.DataSource = m_logic.CurrencyList;
            category1comboBox1.DataSource = m_logic.CustomerCategory1List;
            category2comboBox.DataSource = m_logic.CustomerCategory2List;
            category3comboBox.DataSource = m_logic.CustomerCategory3List;
            priceCategorycomboBox.DataSource = m_logic.PriceCategoryList;
            vendorTypecomboBox.DataSource = getCustomerType();
            labelCategory1.Text = m_mainForm.GetGeneralSetup().CustomerCategory1ColumnName;
            labelCategory2.Text = m_mainForm.GetGeneralSetup().CustomerCategory2ColumnName;
            labelCategory3.Text = m_mainForm.GetGeneralSetup().CustomerCategory3ColumnName;
            taxcomboBox3.Text = ServiceRegistry.GetInstance().GetDefaultDto(taxcomboBox3.DataSource as IList);
            topcomboBox1.Text = ServiceRegistry.GetInstance().GetDefaultDto(topcomboBox1.DataSource as IList);
            currencycomboBox4.Text = ServiceRegistry.GetInstance().GetDefaultDto(currencycomboBox4.DataSource as IList);

        }
        private IList getCustomerType()
        {
            IList customerType = new ArrayList();
            customerType.Add(VendorType.Customer);
            customerType.Add(VendorType.SupplierCustomer);
            return customerType;
        }
        public void Refresh()
        {
            b_init = false;
            Init();
            m_logic.Refresh();
        }

        public void ClearForm()
        {
            textBoxCode.Text = "";
            textBoxName.Text = "";
            addresstextBox.Text = string.Empty;
            citytextBox2.Text = string.Empty;
            provincetextBox7.Text = string.Empty;
            ziptextBox8.Text = string.Empty;
            countrytextBox3.Text = string.Empty;
            contacttextBox1.Text = string.Empty;
            phonetextBox4.Text = string.Empty;
            faxtextBox9.Text = string.Empty;
            emailtextBox5.Text = string.Empty;
            websitetextBox6.Text = string.Empty;
            topcomboBox1.SelectedItem = null;
            employeecomboBox2.SelectedItem = null;
            taxcomboBox3.SelectedItem = null;
            currencycomboBox4.SelectedItem = null;
            taxNotextBox1.Text = string.Empty;
            notetextBox2.Text = string.Empty;
            barcodeTextBox.Text = string.Empty;
            category1comboBox1.SelectedItem = null;
            category2comboBox.SelectedItem = null;
            category3comboBox.SelectedItem = null;
            category1comboBox1.Text = string.Empty;
            category2comboBox.Text = string.Empty;
            category3comboBox.Text = string.Empty;
            vendorTypecomboBox.Text = string.Empty;
            discontinuecheckBox.Checked = false;
            priceCategorycomboBox.Text = string.Empty;
            priceCategorycomboBox.SelectedItem = null;
            creditLimitnumericUpDown.Value = 0m;
            statusBar1.Text = string.Empty;
            taxcomboBox3.Text = ServiceRegistry.GetInstance().GetDefaultDto(taxcomboBox3.DataSource as IList);
            topcomboBox1.Text = ServiceRegistry.GetInstance().GetDefaultDto(topcomboBox1.DataSource as IList);
            employeecomboBox2.Text = ServiceRegistry.GetInstance().GetDefaultDto(employeecomboBox2.DataSource as IList);
            currencycomboBox4.Text = ServiceRegistry.GetInstance().GetDefaultDto(currencycomboBox4.DataSource as IList);
            errorProvider1.SetError(textBoxCode, "");
            errorProvider1.SetError(textBoxName, "");
        }

        public void SetToReadOnly(bool ro)
        {
            textBoxName.Enabled = !ro;
            textBoxCode.Enabled = !ro;
            addresstextBox.Enabled = !ro;
            citytextBox2.Enabled = !ro;
            provincetextBox7.Enabled = !ro;
            ziptextBox8.Enabled = !ro;
            countrytextBox3.Enabled = !ro;
            contacttextBox1.Enabled = !ro;
            phonetextBox4.Enabled = !ro;
            faxtextBox9.Enabled = !ro;
            emailtextBox5.Enabled = !ro;
            websitetextBox6.Enabled = !ro;
            topcomboBox1.Enabled = !ro;
            employeecomboBox2.Enabled = !ro;
            taxcomboBox3.Enabled = !ro;
            currencycomboBox4.Enabled = !ro;
            taxNotextBox1.Enabled = !ro;
            notetextBox2.Enabled = !ro;
            barcodeTextBox.Enabled = !ro;
            category1comboBox1.Enabled = !ro;
            category2comboBox.Enabled = !ro;
            category3comboBox.Enabled = !ro;
            vendorTypecomboBox.Enabled = !ro;
            discontinuecheckBox.Enabled = !ro;
            creditLimitnumericUpDown.Enabled = !ro;
            priceCategorycomboBox.Enabled = !ro;
            bool categoryEnable = m_mainForm.GetGeneralSetup().CustomerCategory;
            category1comboBox1.Enabled = !ro && m_mainForm.GetGeneralSetup().CustomerCategory1 && categoryEnable;
            category2comboBox.Enabled = !ro && m_mainForm.GetGeneralSetup().CustomerCategory2 && categoryEnable;
            category3comboBox.Enabled = !ro && m_mainForm.GetGeneralSetup().CustomerCategory3 && categoryEnable;
        
        }

        public void SetButtonEditMode(Premiere.Window.EditMode editMode)
        {
            toolBarButtonNew.Enabled = buttonNew.Enabled = ((editMode.Equals(EditMode.OK) || editMode.Equals(EditMode.Empty))) && m_mainForm.GetRules(this.Name).New;
            toolBarButtonSave.Enabled = buttonSave.Enabled = ((editMode.Equals(EditMode.Edit) || editMode.Equals(EditMode.Insert))) && m_mainForm.GetRules(this.Name).New;
            toolBarButtonEdit.Enabled = buttonEdit.Enabled = ((editMode.Equals(EditMode.OK))) && m_mainForm.GetRules(this.Name).Edit;
            toolBarButtonDelete.Enabled = buttonDelete.Enabled = ((editMode.Equals(EditMode.OK) || editMode.Equals(EditMode.Edit))) && m_mainForm.GetRules(this.Name).Delete;
            toolBarButtonPrint.Enabled = m_mainForm.GetRules(this.Name).Print;
        }

        public void RemoveItem(long id)
        {
            if (m_items.ContainsKey(id))
            {
                ListViewItem item = (ListViewItem)m_items[id];
                listViewRecords.Items.Remove(item);
            }
        }
        #endregion

        #region IUserControlCustomer Members


        public string Barcode
        {
            get
            {
                return barcodeTextBox.Text;
            }
            set
            {
                barcodeTextBox.Text = value;
            }
        }

        public string Address
        {
            get
            {
                return addresstextBox.Text;
            }
            set
            {
                addresstextBox.Text = value;
            }
        }

        public string City
        {
            get
            {
                return citytextBox2.Text;
            }
            set
            {
                citytextBox2.Text = value;
            }
        }

        public string Province
        {
            get
            {
                return provincetextBox7.Text;
            }
            set
            {
                provincetextBox7.Text = value;
            }
        }

        public string Zipcode
        {
            get
            {
                return ziptextBox8.Text;
            }
            set
            {
                ziptextBox8.Text = value;
            }
        }

        public string Country
        {
            get
            {
                return countrytextBox3.Text;
            }
            set
            {
                countrytextBox3.Text = value;
            }
        }

        public string Contact
        {
            get
            {
                return contacttextBox1.Text;
            }
            set
            {
                contacttextBox1.Text = value;
            }
        }

        public string Phone
        {
            get
            {
                return phonetextBox4.Text;
            }
            set
            {
                phonetextBox4.Text = value;
            }
        }

        public string Fax
        {
            get
            {
                return faxtextBox9.Text;
            }
            set
            {
                faxtextBox9.Text = value;
            }
        }

        public string Email
        {
            get
            {
                return emailtextBox5.Text;
            }
            set
            {
                emailtextBox5.Text = value;
            }
        }

        public string Website
        {
            get
            {
                return websitetextBox6.Text;
            }
            set
            {
                websitetextBox6.Text = value;
            }
        }

        public TOPDTO TOP
        {
            get
            {
                return (TOPDTO)topcomboBox1.SelectedItem;
            }
            set
            {
                topcomboBox1.Text = value == null ? "" : value.ToString();
            }
        }

        public EmployeeMasterDTO Employee
        {
            get
            {
                return (EmployeeMasterDTO)employeecomboBox2.SelectedItem;
            }
            set
            {
                employeecomboBox2.Text = value == null ? "" : value.ToString();
            }
        }

        public TaxDTO Tax
        {
            get
            {
                return (TaxDTO)taxcomboBox3.SelectedItem;
            }
            set
            {
                taxcomboBox3.Text = value == null ? "" : value.ToString();
            }
        }

        public CurrencyDTO Currency
        {
            get
            {
                return (CurrencyDTO)currencycomboBox4.SelectedItem;
            }
            set
            {
                currencycomboBox4.Text = value == null ? "" : value.ToString();
            }
        }

        public string TaxNo
        {
            get
            {
                return taxNotextBox1.Text;
            }
            set
            {
                taxNotextBox1.Text = value;
            }
        }

        public string Note
        {
            get
            {
                return notetextBox2.Text;
            }
            set
            {
                notetextBox2.Text = value;
            }
        }

        public bool Discontinue
        {
            get
            {
                return discontinuecheckBox.Checked;
            }
            set
            {
                discontinuecheckBox.Checked = value;
            }
        }

        public VendorType VendorType
        {
            get
            {
                return (VendorType)vendorTypecomboBox.SelectedItem;
            }
            set
            {
                vendorTypecomboBox.SelectedItem = value;
                vendorTypecomboBox.Text = value.ToString();
            }
        }
        public CustomerCategoryDTO CustomerCategory1
        {
            get
            {
                return (CustomerCategoryDTO)category1comboBox1.SelectedItem;
            }
            set
            {
                category1comboBox1.Text = value == null ? string.Empty : value.ToString();
            }
        }

        public CustomerCategoryDTO CustomerCategory2
        {
            get
            {
                return (CustomerCategoryDTO)category2comboBox.SelectedItem;
            }
            set
            {
                category2comboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }

        public CustomerCategoryDTO CustomerCategory3
        {
            get
            {
                return (CustomerCategoryDTO)category3comboBox.SelectedItem;
            }
            set
            {
                category3comboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }
        public PriceCategoryDTO PriceCategory
        {
            get
            {
                return (PriceCategoryDTO)priceCategorycomboBox.SelectedItem;
            }
            set
            {
                priceCategorycomboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }

        public decimal CreditLimit
        {
            get
            {
                return creditLimitnumericUpDown.Value;
            }
            set
            {
                creditLimitnumericUpDown.Value = value;
            }
        }

        public void UpdateActivity(IList activities)
        {
            listViewActivities.Items.Clear();
            foreach (EventInfo dto in activities)
            {
                ListViewItem item = listViewActivities.Items.Add(dto.TransactionDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(dto.TransactionType.ToString());
                item.SubItems.Add(dto.TransactionNo);
                item.SubItems.Add(dto.Posted.ToString());
                item.Tag = dto;
            }
        }

        public void FocusToCode()
        {
            textBoxCode.Focus();
        }

        public bool IsValidSubject()
        {
            errorProvider1.SetError(textBoxCode, "");
            errorProvider1.SetError(textBoxName, "");
            bool a = textBoxCode.Text == string.Empty;
            bool b = textBoxName.Text == string.Empty;
            if (a) errorProvider1.SetError(textBoxCode, "Please fill Code");
            if (b) errorProvider1.SetError(textBoxName, "Please fill Name");
            return !a && !b;
        }


        public void ClearRecordList()
        {
            m_items.Clear();
            this.listViewRecords.Items.Clear();
        }

        public string Filter
        {
            get { return searchTextBox1.Text; }
        }

        public string FoundRecord
        {
            set { labelResult.Text = value; }
        }

        #endregion

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            CustomerDTO user = (CustomerDTO)listViewRecords.SelectedItem.Tag;
            m_logic.UpdateSubject(user);
        }

        private void searchTextBox1_Search(object sender, EventArgs e)
        {
            if (searchTextBox1.Text == string.Empty)
            {
                return;
            }
            m_logic.Find();
        }

        private void textBoxFilter_KeyUp(object objSender, KeyEventArgs objArgs)
        {
            m_key = objArgs.KeyData;
            if (m_key == Keys.Enter)
            {
                if (searchTextBox1.Text == string.Empty)
                {
                    return;
                }
                m_logic.Find();
            }
        }

        private void linkLabelLoadAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            m_logic.LoadAllCustomer();
        }
    }
}